﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyToDo.Common;
using MyToDo.Common.Events;
using Prism.Events;
using Prism.Services.Dialogs;

namespace MyToDo.Extensions
{
    public static class DialogExtensions
    {
        public static async Task<IDialogResult> Question(
            this IDialogHostService dialogHost,
            string title,
            string content,
            string dialogHostName = "Root"
        )
        {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("Title", title);
            dialogParameters.Add("Content", content);
            dialogParameters.Add("dialogHostName", dialogHostName);
            var dialogResult = await dialogHost.ShowDialog(
                "MsgView",
                dialogParameters,
                dialogHostName
            );
            return dialogResult;
        }

        /// <summary>
        /// 推送等待消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="model"></param>
        public static void UpdateLoading(this IEventAggregator aggregator, UpdateModel model)
        {
            aggregator.GetEvent<UpdateLoadingEvent>().Publish(model);
        }

        /// summary>
        /// 注册等待消息
        ///
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="model"></param>
        public static void Register(this IEventAggregator aggregator, Action<UpdateModel> action)
        {
            aggregator.GetEvent<UpdateLoadingEvent>().Subscribe(action);
        }

        /// <summary>
        /// 注册提示消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="action"></param>
        public static void RegisterMessage(
            this IEventAggregator aggregator,
            Action<MessageModel> action,
            string targetName
        )
        {
            aggregator
                .GetEvent<MessageEvent>()
                .Subscribe(
                    action,
                    ThreadOption.PublisherThread,
                    true,
                    (e) =>
                    {
                        return e.FilterName.Equals(targetName);
                    }
                );
        }

        /// <summary>
        /// 发送提示消息
        /// </summary>
        /// <param name="aggregator"></param>
        /// <param name="message"></param>
        public static void SendMessage(
            this IEventAggregator aggregator,
            string message,
            string targetName = "Main"
        )
        {
            aggregator
                .GetEvent<MessageEvent>()
                .Publish(new MessageModel() { FilterName = targetName, Message = message });
        }
    }
}
